package com.dingding.transfer.stamper.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.dingding.transfer.stamper.constant.EventType;
import com.dingding.transfer.stamper.listener.ContextRefreshedListener;
import com.dingding.transfer.stamper.util.GsonUtil;
import com.dingding.transfer.stamper.util.SpringContextUtil;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/4/9 19:11
 **/
@Slf4j
public class DeptAddHanlder implements EventHandler{
    @Override
    public boolean support(String eventType) {
        if (EventType.DEPT_CREATE.getEventType().equals(eventType)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean handle(JSONObject jsonObject) {
        log.info("部门新增时间回调");
        String deptIds = jsonObject.getString("DeptId");
        String corpId = jsonObject.getString("CorpId");
        List<String> list = GsonUtil.fromJson(deptIds, new TypeToken<List<String>>() {
        }.getType());
        ContextRefreshedListener contextRefreshedListener = SpringContextUtil.getBean(ContextRefreshedListener.class);
        if(CollectionUtil.isNotEmpty(list)){
            list.forEach(deptId -> {
                contextRefreshedListener.addDeptInfo(corpId,deptId);
            });
        }
        return false;
    }
}
